//
//  MZDecimalDemoModel.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/6.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "NSDecimalNumber+MZBase.h"

#import "MZDecimalDemoModel.h"

@implementation MZDecimalDemoModel

- (NSString *)typeStrOfType:(MZDemoOpType)opType {
    
    NSString *typeStr = @"";
    
    if (MZDemoOpTypeAdd == opType) {
        typeStr = @"+";
    } else if (MZDemoOpTypeSub == opType) {
        typeStr = @"-";
    } else if (MZDemoOpTypeMul == opType) {
        typeStr = @"x";
    } else if (MZDemoOpTypeDiv == opType) {
        typeStr = @"/";
    }
    
    return typeStr;
}

- (UIAlertController *)opTypeSettingActionSheet {
    
    NSArray *opItemArray = @[
                             @"+", @"-", @"x", @"/",
                             ];
    
    UIAlertController *tmpAc = [UIAlertController alertControllerWithTitle:@"设置操作类型"
                                                                   message:@"选择加减乘除"
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    __weak typeof(self) weakSelf = self;
    
    for (int i = 0; i < opItemArray.count; i++) {
        
        NSString *tmpStr = opItemArray[i];
        
        UIAlertAction *tmpAction = [UIAlertAction actionWithTitle:tmpStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf setCurOpType:MZDemoOpTypeAdd + i];
        }];
        
        [tmpAc addAction:tmpAction];
    }
    
    return tmpAc;
}

#pragma mark - 

- (void)setCurOpType:(MZDemoOpType)curOpType {
    
    _curOpType = curOpType;
    
    if ([_delegate respondsToSelector:@selector(demoModel:didUpdateOpType:)]) {
        [_delegate demoModel:self didUpdateOpType:_curOpType];
    }
}

#pragma mark -

- (NSString *)decimalCalcWithLeftStr:(NSString *)leftStr rightStr:(NSString *)rightStr {
    
    NSDecimalNumber *leftVal = [NSDecimalNumber mzDecimalNumberFromNSString:leftStr];
    NSDecimalNumber *rightVal = [NSDecimalNumber mzDecimalNumberFromNSString:rightStr];
    
    NSDecimalNumber *result = nil;
    
    if (MZDemoOpTypeAdd == _curOpType) {
        result = [leftVal mzAdd:rightVal];
    } else if (MZDemoOpTypeSub == _curOpType) {
        result = [leftVal mzSub:rightVal];
    } else if (MZDemoOpTypeMul == _curOpType) {
        result = [leftVal mzMul:rightVal];
    } else if (MZDemoOpTypeDiv == _curOpType) {
        result = [leftVal mzDiv:rightVal];
    }
    
    return [result stringValue];
}

- (NSString *)stringCalcWithLeftStr:(NSString *)leftStr rightStr:(NSString *)rightStr {
  
    NSDecimalNumber *leftVal = [NSDecimalNumber mzDecimalNumberFromNSString:leftStr];
    
    NSDecimalNumber *result = nil;
    
    if (MZDemoOpTypeAdd == _curOpType) {
        result = [leftVal mzAddStr:rightStr];
    } else if (MZDemoOpTypeSub == _curOpType) {
        result = [leftVal mzSubStr:rightStr];
    } else if (MZDemoOpTypeMul == _curOpType) {
        result = [leftVal mzMulStr:rightStr];
    } else if (MZDemoOpTypeDiv == _curOpType) {
        result = [leftVal mzDivStr:rightStr];
    }
    
    return [result stringValue];
}

- (NSString *)numberCalcWithLeftStr:(NSString *)leftStr rightStr:(NSString *)rightStr {
 
    NSDecimalNumber *leftVal = [NSDecimalNumber mzDecimalNumberFromNSString:leftStr];
    NSNumber *rightVal = @(rightStr.doubleValue);
    
    NSDecimalNumber *result = nil;
    
    if (MZDemoOpTypeAdd == _curOpType) {
        result = [leftVal mzAddNum:rightVal];
    } else if (MZDemoOpTypeSub == _curOpType) {
        result = [leftVal mzSubNum:rightVal];
    } else if (MZDemoOpTypeMul == _curOpType) {
        result = [leftVal mzMulNum:rightVal];
    } else if (MZDemoOpTypeDiv == _curOpType) {
        result = [leftVal mzDivNum:rightVal];
    }
    
    return [result stringValue];
}

#pragma mark - 

- (NSString *)roundingStrFromValString:(NSString *)valStr scale:(NSInteger)scale {
    
    NSDecimalNumber *oriVal = [NSDecimalNumber mzDecimalNumberFromNSString:valStr];
    NSDecimalNumber *roundVal = [oriVal mzRoundingValWithScale:scale];
    return [roundVal stringValue];
}

@end
